/**
 * Collaborative Story Development Web Application (CSDApp) 
 * Copyright Anton Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.util.HashMap;
import java.util.Map;

/**
 * This class represents the base type of a resource that can be created by the
 * end user.
 *
 * @author Anton Strack
 */
public class Resource {

    /**
     * The id of the specific resource.
     */
    protected int id;
    /**
     * the resource type id that identifies the type of the resource to the rest
     * of the system.
     */
    protected int resourceTypeId;
    /**
     * This map is used to store runtime information that isn't stored in the
     * primary database row.
     */
    protected Map<String, String> derivedAttributes;

    /**
     * No argument constructor provided primarily so the object can be bean.
     */
    public Resource() {
        super();
        this.derivedAttributes = new HashMap<String, String>();
    }

    /**
     * Default constructor providing the minimal setup needed by the bean
     *
     * @param resourceTypeId The type of resource the bean is.
     */
    public Resource(int resourceTypeId) {
        this();
        this.resourceTypeId = resourceTypeId;
    }

    //Full constructor providing initiation of the bean's state
    /**
     * Full constructor providing initiation of the bean's state
     *
     * @param id ID value of the bean
     * @param resourceTypeId The type of resource the bean is.
     */
    public Resource(int id, int resourceTypeId) {
        this.id = id;
        this.resourceTypeId = resourceTypeId;
    }

    @Override
    public String toString() {
        return "Resource{" + "id=" + id + ", resourceTypeId=" + resourceTypeId + "derivedValues=" + this.derivedAttributes.toString() + "}";
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the resourceTypeId
     */
    public int getResourceTypeId() {
        return resourceTypeId;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @param resourceTypeId the resourceTypeId to set
     */
    public void setResourceTypeId(int resourceTypeId) {
        this.resourceTypeId = resourceTypeId;
    }

    /**
     * Get the derived string value matching the key. Derived values are values
     * not stored in the bean's database table. They are either the matching
     * names of id values stored in the bean table or they are other related
     * values.
     *
     * @param key Column name key for retrieving the derived attribute. As long
     * as setDerivedValue() was used to set the derived value, case does not
     * matter for the key.
     * @return the derived attribute value matching the key or an empty string
     * if the key doesn't exist.
     */
    public String getDerivedValue(String key) {
        key = key.toLowerCase();
        if (this.derivedAttributes.containsKey(key)) {
            return this.derivedAttributes.get(key);
        }
        return "";
    }

    /**
     * Get the derived int value matching the key. Derived values are values not
     * stored in the bean's database table. They are either the matching names
     * of id values stored in the bean table or they are other related values.
     * This method attempts to return the derived value as an integer.
     *
     * @param key Column name key for retrieving the derived attribute. As long
     * as setDerivedValue() was used to set the derived value, case does not
     * matter for the key.
     * @return the derived attribute value matching the key or 0 if the key
     * doesn't exist
     */
    public int getDerivedInt(String key) {
        String value;
        key = key.toLowerCase();
        if (this.derivedAttributes.containsKey(key)) {
            value = this.derivedAttributes.get(key);
            if (value != null && value.isEmpty() == false) {
                Integer i = Integer.parseInt(value);
                return i.intValue();
            }
        }
        // return zero if any above condition fails
        return 0;
    }

    /**
     * Set the name of the related id value.
     *
     * @param nameFor the key name used to get the value from the bean.
     * Generally follows the pattern StateName if the bean has a field StateId.
     * @param name the name that is related to the id value
     */
    public void setDerivedValue(String nameFor, String name) {
        // convert the name to lowercase to reduce bugs caused by incorrect case.
        this.derivedAttributes.put(nameFor.toLowerCase(), name);
    }
}
